package com.sctvcloud.dongqu.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;

import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.ui.entity.Clickable;


/**
 * 阅读用户协议和隐私政策
 */
public class UserAgreementDiaFragment extends BaseDialogFragment implements View.OnClickListener {
    private CustomFontTextView cancel_cv, sure_cv;
    private CustomFontTextView content;

    private CustomFontTextView user_agreement, privacy_policy;
    public static final int CANCEL = 0;//退出
    public static final int CONFIRM = 1;//确定
    public static final int AGREEMENT = 2;//用户协议
    public static final int POLICY = 3;//隐私政策
    private boolean isShow = false;


    @Override
    protected int getContentRes() {
        return R.layout.dia_fragment_user_agreement;
    }

    @Override
    protected void initViews(Dialog dialog, Bundle savedInstanceState) {
        cancel_cv = (CustomFontTextView) dialog.findViewById(R.id.dia_show_cancel);
        sure_cv = (CustomFontTextView) dialog.findViewById(R.id.dia_show_sure);
//        user_agreement = (CustomFontTextView) dialog.findViewById(R.id.user_agreement);
//        privacy_policy = (CustomFontTextView) dialog.findViewById(R.id.privacy_policy);

        content = (CustomFontTextView) dialog.findViewById(R.id.content);
        cancel_cv.setOnClickListener(this);
        sure_cv.setOnClickListener(this);
//        user_agreement.setOnClickListener(this);
//        privacy_policy.setOnClickListener(this);

//        user_agreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//        privacy_policy.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        SpannableString spannableString = new SpannableString(getContext().getResources().getString(R.string.user_content1) +
                getContext().getResources().getString(R.string.user_agreement) +
                getContext().getResources().getString(R.string.user_content2) +
                getContext().getResources().getString(R.string.privacy_policy) +
                getContext().getResources().getString(R.string.user_content3));

        spannableString.setSpan(new Clickable(getContext(),new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (callBack != null) {
                    Message message = new Message();
                    message.what = AGREEMENT;
                    callBack.onCallBackMsg(UserAgreementDiaFragment.this, message);
                }
            }
        }, true), getContext().getResources().getString(R.string.user_content1).length(),getContext().getResources().getString(R.string.user_content1).length()+ getContext().getResources().getString(R.string.user_agreement).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(new Clickable(getContext(),new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (callBack != null) {
                            Message message = new Message();
                            message.what = POLICY;
                            callBack.onCallBackMsg(UserAgreementDiaFragment.this, message);
                        }
                    }
                }, true), getContext().getResources().getString(R.string.user_content1).length()
                        + getContext().getResources().getString(R.string.user_agreement).length()
                        + getContext().getResources().getString(R.string.user_content2).length(),
                getContext().getResources().getString(R.string.user_content1).length()
                        + getContext().getResources().getString(R.string.user_agreement).length()
                        + getContext().getResources().getString(R.string.user_content2).length()
                        +getContext().getResources().getString(R.string.privacy_policy).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        content.setText(spannableString);
        content.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private SpannableString getTextWithSpan(String from, String to, String content) {
        if (TextUtils.isEmpty(to)) {
            return new SpannableString(from + ":" + content);
        } else {
            return new SpannableString(from + "回复" + to + ":" + content);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == cancel_cv) {
            if (callBack != null) {
                Message message = new Message();
                message.what = CANCEL;
                callBack.onCallBackMsg(this, message);
                dismissAllowingStateLoss();
            }

        } else if (v == sure_cv) {
            if (callBack != null) {
                Message message = new Message();
                message.what = CONFIRM;
                callBack.onCallBackMsg(this, message);
                dismissAllowingStateLoss();
            }
        }
//        else if (v == user_agreement) {
//            if (callBack != null) {
//                Message message = new Message();
//                message.what = AGREEMENT;
//                callBack.onCallBackMsg(this, message);
//            }
//        } else if (v == privacy_policy) {
//            if (callBack != null) {
//                Message message = new Message();
//                message.what = POLICY;
//                callBack.onCallBackMsg(this, message);
//            }
//        }
    }

    public boolean isShow() {
        return isShow;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isShow = false;
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        super.show(manager, tag);
        isShow = true;
    }


}
